package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.bean.EnumManage;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.CompanyFormEntity;
import com.fz.us.admin.company.entity.Duty;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.CompanyFormService;
import com.fz.us.admin.company.service.OrganizeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service实现类 - 企业表单 基类
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-09-06 。
 * ============================================================================
 */
@Service
public abstract class CompanyFormServiceImpl<T extends CompanyFormEntity> extends BaseServiceImpl<T, String> implements CompanyFormService<T> {

    @Resource
    private OrganizeService organizeService;

    @Override
    public Result createEntity(User user, Duty duty, T entity) {
        Assert.notNull(user, "user is required");
        Assert.notNull(entity, "entity is required");

        // 设置所属企业
        entity.setCompany(user.getCompany());
        // 设置创建人
        entity.setCreater(user);
        // 设置创建人职权&部门
        if (duty != null) {
            entity.setCreaterDuty(duty);
            entity.setCreaterDepartment(duty.getPower().getDepartment());
        }

        return saveEntity(entity);
    }

    @Override
    public SearchResultPaging findCompanyPagingBySearch(PagePaging page, List<Rule> rules, Order order, Company company, User user) {
        Assert.notNull(company, "company is required");
        Assert.notNull(user, "user is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        // 默认条件
        rules.add(RuleHelper.eq("state", BaseEnumManager.StateEnum.Enable));
        rules.add(RuleHelper.eq("company", company));


        // 部门权限过滤 -> 部门内及上级可见
        List<Duty> dutyList = organizeService.getUserDutyList(user);
        if (dutyList!=null && dutyList.size()>0) {
            Rule[] ruleArray = new Rule[dutyList.size()];
            for (int i=0;i<dutyList.size();i++) {
                ruleArray[i] = RuleHelper.like("createrDepartment.path", dutyList.get(i).getPower().getDepartment().getId());
            }
            rules.add(RuleHelper.or(ruleArray));
        } else {
            rules.add(RuleHelper.isNull("createrDepartment"));
        }


        // 默认排序 - 创建时间 降序
        if (order == null)
            order = Order.order("createTime", SearchEnum.OrderType.desc);

        return findPagingBySearch(page, rules, order);
    }

}
